package com.mall.content.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.manager.dao.TbContentMapper;
import com.mall.manager.pojo.TbContent;
import com.mall.manager.pojo.TbContentExample;
import com.mall.pojo.EasyUiPageBean;
import com.mall.pojo.TaotaoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;


/**
 * @author Administrator
 */
@Service
@Component
@Transactional
@CacheConfig(cacheNames = "content",cacheManager = "redisCacheManager")
public class ContentServiceImp implements com.mall.content.service.ContentService {

    @Autowired
    private TbContentMapper contentMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public TaotaoResult addContent(TbContent content) {
        Date date = new Date();
        content.setCreated(date);
        content.setUpdated(date);
        contentMapper.insert(content);
        TaotaoResult ok = TaotaoResult.ok();

        clearAllPageQueryCache();

        return ok;
    }

    private void clearAllPageQueryCache(){
        //清除原有的缓存
        redisTemplate.delete(redisTemplate.keys("content:" + "*"));
    }

    @Override
    @Cacheable(keyGenerator = "MykeyGenerator")
    public EasyUiPageBean pageQuery(int page, int rows, long categoryId) {
        PageHelper.startPage(page,rows);

        TbContentExample contentExample = new TbContentExample();
        TbContentExample.Criteria criteria = contentExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> contents = contentMapper.selectByExample(contentExample);

        /*设置分页查询结果*/
        EasyUiPageBean result = new EasyUiPageBean();
        result.setRows(contents);

        /*设置总条数*/
        PageInfo<TbContent> pageInfo = new PageInfo<>(contents);
        long total = pageInfo.getTotal();
        result.setTotal(total);
        return result;
    }

    /**
     * 一般在service层加上缓存
     * @param cid
     * @return
     *
     */
    @Override
    @Cacheable(key ="#cid")
    public List<TbContent> listIndexContents(long cid) {
        TbContentExample contentExample = new TbContentExample();
        TbContentExample.Criteria criteria = contentExample.createCriteria();
        criteria.andCategoryIdEqualTo(cid);
        List<TbContent> contents = contentMapper.selectByExampleWithBLOBs(contentExample);
        return contents;
    }

    /**
     * 数据库的同时也要更新缓存
     * @param newContent
     */
    @Override
    public void update(TbContent newContent) {
        TbContent oldc = contentMapper.selectByPrimaryKey(newContent.getId());
        newContent.setUpdated(new Date());
        newContent.setCreated(oldc.getCreated());
        newContent.setCategoryId(oldc.getCategoryId());
        contentMapper.updateByPrimaryKey(newContent);

        clearAllPageQueryCache();
    }

    /**
     * 删除数据库的同时也删除缓存
     * @param ids
     */
    @Override
    public void deleteContent(Long[] ids) {
        clearAllPageQueryCache();
        for (Long id : ids) {
            contentMapper.deleteByPrimaryKey(id);
        }
    }
}
